Now that we have a wave loop, we can fire off things in the world periodically, like our volcano eruption.
At certain points of time, we could have waves of monsters coming, things like that.
That's pretty cool.
Let's start out with a wave of monsters.
I'll use the drooling zombies again until we have our own monsters.
But this time, what I'm going to do is I'm going to control the location at which they spawn.
So I'm going to add a part, and this is going to be a spawn location.
I'm going to say Bon Logue.
We can call it like an m spawn load for monster spawn.
Luke.
How's that?
All right, now let's duplicate that.
Collisions are off, so if I select it, control D, I duplicate in place and we'll do another one.
Control D and another one control D.
Obviously, you'd want to put these in different parts of your game, but this will be good.
For example, like you might want to be in a room somewhere and have the zombies come out from all sides.
That would be cool.
All right.
Let's go ahead and select all of our M spawn locks.
So select the top shift, click the bottom, select some.
All right, click and group as model or folder.
They'll both work.
They'll both work with no code changes.
All right.
I'm going to call this m spawn locks with an SX.
Now I'm going to open that up.
I'm going to change.
Select the M spawn lock at the top shift, click the bottom.
So they're all selected.
I make these invisible.
I don't want to see them transparency or change out to one and now I don't want to trip over them.
So I'm going to go down and can Clyde turn that off?
Since canned collides off, we don't want them to fall through the ground anchored on Cool.
There we go.
We got some spawn locations.
Let's go over to our session utils and we'll start working on a function to spawn a wave of zombies.
So I'll say local function.
No local.
I'm sorry, function window local utils spawn.
I use a lowercase spawn zombies and I want the number of zombies to spawn number of zombies.
Cool.
Now I need to have a zombie to spawn and server storage, so I'm going to do it.
So we have some in here already.
We have our simple Z spanner.
Right.
I can just duplicate one of these control D and I'm going to drag it outside of my simple Z spinner.
Like if I close that, it's going to be on the same level as my land, mine and my coin and my health
pickup.
All right, now I can get it easy.
All right, so this is where this is unaffected.
The simple Z spanner is unaffected.
I want to get a reference to server storage.
I'll call it SAS.
That's my variable for server storage.
So as SAS equals gain, get service server storage.
I also want a random number generator to select my spawn location, so I'll do a local R and D.
That's going to be my random number generator, random dot new.
And here you can leave that blank.
Or you could put this tick value.
It's actually a seed value, right?
You put a number in there that changes all the time and you get a different sequence of pseudo generated
random numbers.
All right, So now we're down here in the spawn Zombies.
We have server storage.
We need to get that template zombie to clone.
So I'm going to say local t zombie for template.
Zombie in server storage.
Wait for child drooling zombie.
There we go.
It is this one right here, right under server storage.
So we don't want to use that one.
We want to clone it.
Right.
Just keep that one there.
Also, I need to get a spawn array so I'll call a spawn array equals.
Well, in our workspace we have our MN spawn locus and in there we have spawned locations and spawn
locations for monster spawn.
Let's do a get children that's going to get all of our all of our things that are underneath and spawn
loops which happen to be spawn locations and we're going to store it in that array.
It's a table, but we're storing it in an array like structure, right?
There's no key value pair.
It's just straight array of values.
So I'm going to make a for loop for I and for I equals one to the number of zombies in steps of one
do local.
We will create a zombie by using the DT zom template and cloning it.
Unlike Java, this is a deep clone, right?
So it makes it exact copy.
A Java.
You have to override.
You have have to override the method.
All right.
Now, our zombie parent needs to be the zombie needs to be in the workspace so you can make the parent
the workspace or some child of the workspace.
We'll just make it the workspace for now.
All right.
I'm going to need an index for my spawn location.
This is an array, so we can access it with a number from 1 to 4 because there's four elements.
I'll do the random number generator.
I'll get the next integer, which is a random integer between one and then number of values in spawn
array.
Right?
So this is like spawn array length.
If you've done other languages that use the length function, this is the number of elements in this
array.
It doesn't work for key value pairs, but it does work if you're just have a table.
It's a list like structure, right?
Or an array like structure.
Right now I'm going to get my zombie that I that I cloned.
I'm going to move to my spawn arrays location.
These are square brackets.
Oh, I want index index dot possession.
Right.
So each one of these is one of the spawn locations in the array.
Right?
That's a number between one and four.
We want the position.
We're going to move to it now.
It used to be that you had to have a primary part selected in your model if you wanted to use the move
to.
That's not the case anymore.
But just for curiosity, go to your drooling zombie.
Take a look here.
Primary part.
That's what you had to do.
So usually the humanoid root parts, the primary part, you would select this.
Then.
You're not going to have any problems, though.
They they made that easier.
They made it more, more friendly.
All right.
Now I'm just going to do a weight.
And that way we can see the zombie spawning from the different locations.
Right.
So it's going to loop through the number.
Now, I just need to call this Where do I call it?
I call it from my wave loop.
I'm going to copy this, so don't spell it wrong.
Copy.
I'm going to go to my wave loop.
Remember, it's in server script service, wave loop.
But I also have it opened here.
There we go.
And in the wave loop, I'll just wait a second.
And then spawned zombies.
Oh, we don't have a number of zombies defined.
Do a control C to copy that.
Just make that a variable up here.
Control V number of zombies and we'll make it five.
All right, let's try it out.
Let's see what we got.
I think what I'm going to do.
Is when I start the game.
I'll just wait 5 seconds to get before we get the loop going.
Right now, the way we can see things happen a little more.
I want to do a play here.
So I don't have to run all the way over from my teleporter.
Here we go.
And our locations are in here.
So there we go.
There's one.
Two came out over there.
There's one.
So we're spawning zombies every 2 seconds.
We make it through that loop, and then we spawn five of them, which takes 5 seconds.
So that's why they're kind of coming out in that way.
It doesn't really look like a wave yet, but we can fix that.
They're going to keep coming.
If you're curious of how many zombies you can get in the world, I believe it's still 255.
It's the animations that run out first, right?
So it's either like 255 or 256.
It's like two to the eighth power for the animations.
All right.
So I will see you in the next video.
We're going to start adding some more stuff to our wave loop.